package com.itbaizhan.dao.impl;

import com.itbaizhan.common.JdbcDruidUtil;
import com.itbaizhan.dao.UsersDao;
import com.itbaizhan.pojo.Items;
import com.itbaizhan.pojo.Orders;
import com.itbaizhan.pojo.Users;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class UsersDaoImpl extends BaseDaoImpl implements UsersDao {
    /*根据用户id 查询用户相关信息*/
    public Users selectById(int userid){
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        Users users=null;
        try{
            conn= JdbcDruidUtil.getConnection();

            ps= conn.prepareStatement("select* from users where userid=?");
        ps.setInt(1,userid);
            rs=ps.executeQuery();
        while(rs.next()){
            users=new Users();
            users.setUserid(rs.getInt("userid"));
            users.setUsername(rs.getString("username"));
            users.setUserage(rs.getInt("userage"));
        }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
JdbcDruidUtil.closeResource(rs,ps,conn);
        }
        return users;
    }

    @Override
    public int updateUsersById(Users users) {
      String sql="update users set userage=? where userid=?";
      Object[] param=new Object[]{users.getUserage(),users.getUserid()};
    return this.executeUpdate(sql,param);

    }
    public List<Users> selectUsersByLikeName(String username){
        String sql="select* from users where username like ?";
        Object[] param =new Object[]{"%"+username+"%"};
        return  this.select(sql,param,Users.class);

    }

    @Override
    public Users selectUsers(int userid) {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        Users users=new Users();

        try{
            conn=JdbcDruidUtil.getConnection();
            ps=conn.prepareStatement("select * from users u,orders o, orders_items oi, items i WHERE\n" +
                    "u.userid = o.user_id and o.orderid = oi.order_id and oi.item_id = i.itemid \n" +
                    "and u.userid =?");
            ps.setInt(1,userid);
            rs=ps.executeQuery();
            while(rs.next()){
                users.setUserid(rs.getInt("userid"));
                users.setUsername(rs.getString("username"));
                users.setUserage(rs.getInt("userage"));
                Orders orders=new Orders();
                orders.setOrderid(rs.getInt("userid"));
                orders.setOrderprice(rs.getDouble("orderprice"));
            users.getOrders().add(orders);
                Items items = new Items();
                items.setItemid(rs.getInt("itemid"));
                items.setItemname(rs.getString("itemname"));
                items.setItemprice(rs.getDouble("itemprice"));
                items.setItemnum(rs.getInt("itemnum"));
                orders.getItems().add(items);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally{
            JdbcDruidUtil.closeResource(rs,ps,conn);
        }
        return users ;
    }

}
